package com.ocean.web.controller.system;

import java.util.Base64;
import java.util.List;

import com.ocean.common.core.domain.entity.SysUser;
import com.ocean.product.domain.OProduct;
import com.ocean.product.service.IOProductService;
import com.ocean.system.domain.OZnwgSq;
import com.ocean.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ocean.common.annotation.Log;
import com.ocean.common.enums.BusinessType;
import com.ocean.system.domain.OProductSmsphone;
import com.ocean.system.service.IOProductSmsphoneService;
import com.ocean.common.core.controller.BaseController;
import com.ocean.common.core.domain.AjaxResult;
import com.ocean.common.utils.poi.ExcelUtil;
import com.ocean.common.core.page.TableDataInfo;

/**
 * 短信提醒电话Controller
 * 
 * @author ocean
 * @date 2025-04-10
 */
@Controller
@RequestMapping("/system/smsphone")
public class OProductSmsphoneController extends BaseController
{
    private String prefix = "system/smsphone";
    private String prefixznwg = "system/smsphoneznwg";

    @Autowired
    private IOProductSmsphoneService oProductSmsphoneService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private IOProductService oProductService;

    @RequiresPermissions("system:smsphone:view")
    @GetMapping()
    public String smsphone(ModelMap mmap)
    {
        OProduct oProduct = new OProduct();
        List<OProduct> list = oProductService.selectFormList(oProduct);
        mmap.put("list",list);
        return prefix + "/smsphone";
    }

    @GetMapping("/znwgdx")
    public String znwgdx()
    {
        return prefixznwg + "/smsphone";
    }

    /**
     * 查询短信提醒电话列表
     */
    @RequiresPermissions("system:smsphone:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(OProductSmsphone oProductSmsphone)
    {
        startPage();
        oProductSmsphone.setType(0);
        List<OProductSmsphone> list = oProductSmsphoneService.selectOProductSmsphoneList(oProductSmsphone);
        return getDataTable(list);
    }
    /**
     * 查询短信提醒电话列表智能网格
     */
    @PostMapping("/listznwg")
    @ResponseBody
    public TableDataInfo listznwg(OProductSmsphone oProductSmsphone)
    {
        startPage();
        oProductSmsphone.setType(1);
        List<OProductSmsphone> list = oProductSmsphoneService.selectOProductSmsphoneList(oProductSmsphone);
        return getDataTable(list);
    }

    /**
     * 导出短信提醒电话列表
     */
    @RequiresPermissions("system:smsphone:export")
    @Log(title = "短信提醒电话", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(OProductSmsphone oProductSmsphone)
    {
        oProductSmsphone.setType(0);
        List<OProductSmsphone> list = oProductSmsphoneService.selectOProductSmsphoneList(oProductSmsphone);
        ExcelUtil<OProductSmsphone> util = new ExcelUtil<OProductSmsphone>(OProductSmsphone.class);
        return util.exportExcel(list, "短信提醒电话数据");
    }

    /**
     * 智能网格导出短信提醒电话列表
     * @param oProductSmsphone
     * @return
     */
    @Log(title = "短信提醒电话", businessType = BusinessType.EXPORT)
    @PostMapping("/exportznwg")
    @ResponseBody
    public AjaxResult exportznwg(OProductSmsphone oProductSmsphone)
    {
        oProductSmsphone.setType(1);
        List<OProductSmsphone> list = oProductSmsphoneService.selectOProductSmsphoneList(oProductSmsphone);
        ExcelUtil<OProductSmsphone> util = new ExcelUtil<OProductSmsphone>(OProductSmsphone.class);
        return util.exportExcel(list, "短信提醒电话数据");
    }

    /**
     * 添加联系人
     * @param pid
     * @param phone
     * @return
     */
    @PostMapping("/addsj")
    @ResponseBody
    public AjaxResult addsj(String pid, String phone)
    {
        int i = 0;
        String[] cpid = pid.split(",");
        for (int j = 0; j < cpid.length; j++) {
            for (String s : phone.split(",")) {
                SysUser sysUser = userService.selectUserById(Long.decode(s));//根据用户id查询用户数据
                OProductSmsphone select = new OProductSmsphone();
                select.setProductId(cpid[j]);
                select.setName(sysUser.getLoginName());
                List<OProductSmsphone> oProductSmsphones = oProductSmsphoneService.selectOProductSmsphoneList(select);//查询是否添加过这个用户 如果有不添加
                String addname = "";
                if(oProductSmsphones.size()>0){//如果存在不添加

                }else{
                    OProductSmsphone oProductSmsphone = new OProductSmsphone();
                    oProductSmsphone.setType(0);
                    oProductSmsphone.setProductId(cpid[j]);

                    oProductSmsphone.setName(sysUser.getLoginName());
                    // 创建解码器
                    Base64.Decoder decoder = Base64.getDecoder();
                    // 执行解密
                    byte[] decodedBytes = decoder.decode(sysUser.getPhonenumber());
                    String decodedStr = new String(decodedBytes);
                    oProductSmsphone.setPhone(decodedStr);
                    i = oProductSmsphoneService.insertOProductSmsphone(oProductSmsphone);
                }

            }
        }
        System.out.println(pid);
        System.out.println(phone);
        return toAjax(i);
    }

    /**
     * 添加智能网格联系人
     * @param pid
     * @param phone
     * @return
     */
    @PostMapping("/addsjznwg")
    @ResponseBody
    public AjaxResult addsjznwg(String pid, String phone)
    {
        int i = 0;
        String[] cpid = pid.split(",");
        for (int j = 0; j < cpid.length; j++) {
            for (String s : phone.split(",")) {
                SysUser sysUser = userService.selectUserById(Long.decode(s));//根据用户id查询用户数据
                OProductSmsphone select = new OProductSmsphone();
                select.setProductId(cpid[j]);
                select.setName(sysUser.getLoginName());
                List<OProductSmsphone> oProductSmsphones = oProductSmsphoneService.selectOProductSmsphoneList(select);//查询是否添加过这个用户 如果有不添加
                String addname = "";
                if (oProductSmsphones.size() > 0) {//如果存在不添加

                } else {
                    OProductSmsphone oProductSmsphone = new OProductSmsphone();
                    oProductSmsphone.setType(1);
                    oProductSmsphone.setProductId(cpid[j]);

                    oProductSmsphone.setName(sysUser.getLoginName());
                    // 创建解码器
                    Base64.Decoder decoder = Base64.getDecoder();
                    // 执行解密
                    byte[] decodedBytes = decoder.decode(sysUser.getPhonenumber());
                    String decodedStr = new String(decodedBytes);
                    oProductSmsphone.setPhone(decodedStr);
                    i = oProductSmsphoneService.insertOProductSmsphone(oProductSmsphone);
                }

            }
        }
        System.out.println(pid);
        System.out.println(phone);
        return toAjax(i);
    }
    /**
     * 新增短信提醒电话
     */
    @GetMapping("/add/{pid}")
    public String add(@PathVariable("pid") String pid, ModelMap mmap)
    {
        List<SysUser> getuserphone = userService.getuserphone();
        OProduct oProduct = new OProduct();
        List<OProduct> list = oProductService.selectFormList(oProduct);
        mmap.addAttribute("data",getuserphone);//联系人下拉框
        mmap.addAttribute("cp",list);//产品下拉框
        mmap.addAttribute("pid",pid);
        return prefix + "/add";
    }
    @GetMapping("/addznwg/{pid}")
    public String addznwg(@PathVariable("pid") String pid, ModelMap mmap)
    {
        List<SysUser> getuserphone = userService.getuserphone();
        List<OZnwgSq> list = oProductService.selectFormListznwg();
        mmap.addAttribute("data",getuserphone);
        mmap.addAttribute("pid",pid);
        mmap.addAttribute("cp",list);
        return prefixznwg + "/add";
    }
    /**
     * 新增保存短信提醒电话
     */
    @RequiresPermissions("system:smsphone:add")
    @Log(title = "短信提醒电话", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(OProductSmsphone oProductSmsphone)
    {
        oProductSmsphone.setType(0);
        return toAjax(oProductSmsphoneService.insertOProductSmsphone(oProductSmsphone));
    }

    @Log(title = "短信提醒电话", businessType = BusinessType.INSERT)
    @PostMapping("/addznwg")
    @ResponseBody
    public AjaxResult addSaveznwg(OProductSmsphone oProductSmsphone)
    {
        oProductSmsphone.setType(1);
        return toAjax(oProductSmsphoneService.insertOProductSmsphone(oProductSmsphone));
    }

    /**
     * 修改短信提醒电话
     */
    @RequiresPermissions("system:smsphone:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        OProductSmsphone oProductSmsphone = oProductSmsphoneService.selectOProductSmsphoneById(id);
        mmap.put("oProductSmsphone", oProductSmsphone);
        return prefix + "/edit";
    }
    @GetMapping("/editznwg/{id}")
    public String editznwg(@PathVariable("id") Long id, ModelMap mmap)
    {
        OProductSmsphone oProductSmsphone = oProductSmsphoneService.selectOProductSmsphoneById(id);
        mmap.put("oProductSmsphone", oProductSmsphone);
        return prefixznwg + "/edit";
    }

    /**
     * 修改保存短信提醒电话
     */
    @RequiresPermissions("system:smsphone:edit")
    @Log(title = "短信提醒电话", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(OProductSmsphone oProductSmsphone)
    {
        return toAjax(oProductSmsphoneService.updateOProductSmsphone(oProductSmsphone));
    }

    @Log(title = "短信提醒电话", businessType = BusinessType.UPDATE)
    @PostMapping("/editznwg")
    @ResponseBody
    public AjaxResult editSaveznwg(OProductSmsphone oProductSmsphone)
    {
        return toAjax(oProductSmsphoneService.updateOProductSmsphone(oProductSmsphone));
    }

    /**
     * 删除短信提醒电话
     */
    @Log(title = "短信提醒电话", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(oProductSmsphoneService.deleteOProductSmsphoneByIds(ids));
    }

    @PostMapping( "/removeznwg")
    @ResponseBody
    public AjaxResult removeznwg(String ids)
    {
        return toAjax(oProductSmsphoneService.deleteOProductSmsphoneByIds(ids));
    }

    /**
     * 智能网格启用关闭
     * @param id
     * @return
     */
    @PostMapping( "/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(Integer id,Integer status)
    {
        int status1 = oProductSmsphoneService.upchangeStatus(id,status);
        return toAjax(status1);
    }
    /**
     * 产品启用关闭
     * @param id
     * @return
     */
    @PostMapping( "/changeStatuscp")
    @ResponseBody
    public AjaxResult changeStatuscp(String id,Integer status)
    {
        int status1 = oProductSmsphoneService.upchangeStatuscp(id,status);
        return toAjax(status1);
    }
}
